package {
	import org.flixel.FlxSprite;
	import org.flixel.FlxGroup;
	import states.GamePlayState;
	import org.flixel.FlxG;
	import org.flixel.FlxBasic;
	/**
	 * @author NilsIH
	 */
	public class NiloCursor extends FlxGroup{
		
		[Embed(source="../img/cursor.png")]
		private var CursorGFX : Class;
		
		
		private var _charge:Number = 0;
		private var _maxCharge:Number = 8;
		private var _cursorBase:FlxSprite;
		private var _cursorCharge:FlxSprite;
		private var _cursorLoop:FlxSprite;
		private var _shakeRatio:Number;
		private var _initialPress:Boolean;
		
		public function NiloCursor(){
			_cursorBase = new FlxSprite();
			_cursorBase.loadGraphic(CursorGFX, true, false, 48, 40);
			_cursorBase.frame = 17;
			
			_cursorCharge = new FlxSprite();
			_cursorCharge.loadGraphic(CursorGFX, true, false, 48, 40);
			_cursorCharge.frame = 6;
			
			_cursorLoop = new FlxSprite();
			_cursorLoop.loadGraphic(CursorGFX, true, false, 48, 40);
			_cursorLoop.addAnimation("cursorloop", [0, 1, 2, 3, 4, 5], 10);
			_cursorLoop.play("cursorloop");
			
			_initialPress = false;
			
			add(_cursorBase);
			add(_cursorCharge);
			add(_cursorLoop);
		}
		
		public function fullCharge():void {
			_charge = _maxCharge;
		}
		
		public function get initialPress():Boolean {
			return _initialPress;
		}
		
		public function getAction():String {
			var result:String = "infest";
			var frame:int = Math.round((_charge / _maxCharge) * 10);
			if (frame > 7){
				result = "explode";
				FlxG.play(AssetStore.Explod);
				if (!_initialPress){
					FlxG.play(AssetStore.Hyl1, .3);
				}
			}
			else {
				FlxG.play(AssetStore.infect);
			}
			_cursorBase.flicker(.2);
			_cursorCharge.flicker(.2);
			_cursorLoop.flicker(.2);
			_charge = 0;
			_initialPress = true;
			return result;
		}
		
		override public function update():void {
			super.update();
			_shakeRatio = 0;
			if (_initialPress){
				_charge += FlxG.elapsed;
				
				if (_charge >= _maxCharge){
					GamePlayState.fireClick();
				}
			}	
			var frame:int = Math.round((_charge / _maxCharge) * 10);
				_cursorCharge.frame = 6 + frame;
				if (frame > 7){
					_shakeRatio = (frame - 7 / 3);
				}
			var x:Number = FlxG.mouse.x - 10 + (((FlxG.random() * 2) - 1) * _shakeRatio);
			var y:Number = FlxG.mouse.y - 6;
			_cursorBase.x = x;
			_cursorBase.y = y;
			_cursorCharge.x = x;
			_cursorCharge.y = y;
			_cursorLoop.x = x;
			_cursorLoop.y = y;	
		}
	}
}
